//
//  ResetGestureViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/14.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "ResetGestureViewController.h"
#import "GestureViewController.h"
#import "PCCircleViewConst.h"
#import "PCCircleView.h"
#import "PCLockLabel.h"
#import <Masonry.h>
#import "VerifyViewController.h"
#import "PCCircleInfoView.h"
#import "PCCircle.h"
#import "LoginViewController.h"
#import "NavigateViewController.h"


@interface ResetGestureViewController ()<CircleViewDelegate>

/**
 *  文字提示Label
 */
@property (nonatomic, strong) PCLockLabel *msgLabel;

@property(nonatomic,strong)UIButton *pwdLoginBtn;


/**
 *  infoView
 */
@property (nonatomic, strong) PCCircleInfoView *infoView;

@end

@implementation ResetGestureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    if (self.type == openTouchId || self.type == closeGesture) {
        self.title = @"验证手势密码";
    }else
    {
        self.title = @"重设手势密码";
    }
    
    
    //创建View
    [self createView];
    [self.pwdLoginBtn addTarget:self action:@selector(pwdLoginBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
}
-(void)pwdLoginBtnAction
{
    NSLog(@"点击密码登录");
    VerifyViewController *verifyVC = [[VerifyViewController alloc] init];
    [self.navigationController pushViewController:verifyVC animated:YES];
}

-(void)createView
{
    
    self.msgLabel = [[PCLockLabel alloc] init];
    [self.msgLabel showNormalMsg:gestureTextOldGesture];
    [self.view addSubview:self.msgLabel];
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(150);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(kScreenW);
        make.height.mas_equalTo(14);
    }];
    
    PCCircleView *lockView = [[PCCircleView alloc] init];
    lockView.delegate = self;
    [lockView setType:CircleViewTypeVerify];
    [self.view addSubview:lockView];
    
    [lockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.msgLabel.mas_bottom).offset(30);
        make.left.mas_equalTo(40);
        make.width.mas_equalTo(self.view.frame.size.width - 80);
        make.height.equalTo(lockView.mas_width);
    }];
    
    self.pwdLoginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.pwdLoginBtn setTitle:@"验证登录密码" forState:(UIControlStateNormal)];
    [self.pwdLoginBtn setTitleColor:[UIColor colorWithRed:91 / 255.0 green:113 / 255.0 blue:255 / 255.0 alpha:1] forState:(UIControlStateNormal)];
    self.pwdLoginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.pwdLoginBtn];
    [self.pwdLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(lockView.mas_bottom).offset(40);
    }];
    
    
    PCCircleInfoView *infoView = [[PCCircleInfoView alloc] init];
    [self.view addSubview:infoView];
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60 + 64);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(CircleRadius * 2 * 0.6);
        make.height.mas_equalTo(CircleRadius * 2 * 0.6);
    }];
}


#pragma mark - login or verify gesture
- (void)circleView:(PCCircleView *)view type:(CircleViewType)type didCompleteLoginGesture:(NSString *)gesture result:(BOOL)equal
{
    // infoView展示对应选中的圆
    [self infoViewSelectedSubviewsSameAsCircleView:view];
    
    
    if (type == CircleViewTypeVerify) {
        
        if (equal) {
            NSLog(@"验证成功");
            if (self.type == ResetGesture) {
                GestureViewController *gestureVc = [[GestureViewController alloc] init];
                [gestureVc setType:GestureViewControllerTypeChange];
                [self.navigationController pushViewController:gestureVc animated:YES];
            }else if (self.type == openTouchId)
            {
                //验证成功  设置开启touchID状态
                [PCCircleViewConst saveFingerprint:@"1" Key:fingerprintStatueKey];
                [self.navigationController popViewControllerAnimated:YES];
            }else if (self.type == closeGesture)
            {
                [PCCircleViewConst saveGesture:@"" Key:gestureFinalSaveKey];
             [self.navigationController popViewControllerAnimated:YES];
                
            }
            
            
        } else {
            NSLog(@"密码错误！");
            
            //删除本地记录手势、退出到登录页面
            [self checkGesterWrongTimes];
            
            
            if (self.type == closeGesture) {
                
                
                
            }else if (self.type == openTouchId)
            {
                //验证成功  设置开启touchID状态
                [PCCircleViewConst saveFingerprint:@"0" Key:fingerprintStatueKey];
            }
        }
    }
}

-(void)checkGesterWrongTimes
{
    //先取值
    if ([AppTools getGesterWrongTimes] == 0) {
        
        static NSInteger i = 0 ;
        i++;
        [AppTools setGesterWrongTimes:i];
    }else
    {
        NSInteger index = [AppTools getGesterWrongTimes] + 1;
        [AppTools setGesterWrongTimes:index];
        if ([AppTools getGesterWrongTimes] == 5) {
            NSLog(@"错误了5次了");
            [AppTools setGesterWrongTimes:0];
            [PCCircleViewConst saveGesture:nil Key:gestureFinalSaveKey];
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"手势密码已失效" message:@"您已连续输错5次手势密码,\n请密码登录" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"密码登录" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                LoginViewController *loginVC = [[LoginViewController alloc] init];
                loginVC.type = LoginVCFromGesterVerifyVC;
                NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:loginVC];
                [self presentViewController:nav animated:YES completion:nil];
            }];
            [alertVC addAction:okAction];
            [self presentViewController:alertVC animated:YES completion:nil];
            
            
        }
    }
    NSInteger times = 5 - [AppTools getGesterWrongTimes];
    [self.msgLabel showWarnMsgAndShake:[NSString stringWithFormat:@"手势密码错误,还可以在输入%ld次",(long)times]];
}

- (void)infoViewSelectedSubviewsSameAsCircleView:(PCCircleView *)circleView {
    for (PCCircle *circle in circleView.subviews) {
        
        if (circle.state == CircleStateSelected || circle.state == CircleStateLastOneSelected) {
            
            for (PCCircle *infoCircle in circleView.subviews) {
                if (infoCircle.tag == circle.tag) {
                    [infoCircle setState:CircleStateSelected];
                }
            }
        }
    }
}


@end
